//
//  RetrievePasswordViewController.m
//  HandheldHospital
//
//  Created by zhangchao on 16/3/2.
//  Copyright © 2016年 zhangchao. All rights reserved.
//

#import "RetrievePasswordViewController.h"
#import <Masonry/Masonry.h>
#import "NSString+ZC_Regex.h"
#import <AVOSCloud/AVOSCloud.h>

#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height

@interface RetrievePasswordViewController ()<UITextFieldDelegate>
@property (nonatomic,strong) UITextField *emailTF;
@end

@implementation RetrievePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"找回密码";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStyleDone target:self action:@selector(rightAction:)];
    [self addViews];
}

//页面布局
- (void)addViews
{
    self.emailTF = [[UITextField alloc] init];
    self.emailTF.placeholder = @"请输入邮箱";
    self.emailTF.layer.borderWidth = 0.5;
    self.emailTF.layer.cornerRadius = 5;
    self.emailTF.delegate = self;
    [self.view addSubview:self.emailTF];
    [self.emailTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(kHeight * 0.15);
        make.left.equalTo(self.view).offset(kWidth * 0.05);
        make.right.equalTo(self.view).offset(-(kWidth * 0.05));
        make.height.equalTo(@40);
    }];
}

//提交按钮事件
- (void)rightAction:(UIBarButtonItem *)sender
{
    NSString *str = self.emailTF.text;
    if([str isEmail])
    {
        [AVUser requestPasswordResetForEmailInBackground:str block:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                [self showAlertOneButtonWithTitle:@"提示" AndMessage:@"请登陆邮箱进行密码重置" AndAlertStyle:UIAlertControllerStyleAlert AndOneButtonTitle:@"确定" AndBlock:^{
                    [self.navigationController popViewControllerAnimated:YES];
                }];
            } else {
                [self showLabelWithString:@"未知错误" AndBlock:nil];
            }
        }];
    }else
    {
        [self showLabelWithString:@"请输入正确的邮箱" AndBlock:nil];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
